package com.iretailer.report.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iretailer.report.base.SuperMapper;
import com.iretailer.report.model.RenewalRecord;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

public interface RenewalRecordMapper extends SuperMapper<RenewalRecord> {

    @Results({
            @Result(column = "mail", property = "mail"),
            @Result(column = "companyName", property = "companyName", javaType = String.class),
            @Result(column = "companyName", property = "companyName", javaType = String.class),
            @Result(column = "companyId", property = "companyId", javaType = String.class),
            @Result(column = "mallName", property = "mallName", javaType = String.class),
            @Result(column = "mallId", property = "mallId", javaType = String.class),
            @Result(column = "contractStartDate", property = "contractStartDate", javaType = LocalDate.class),
            @Result(column = "contractEndDate", property = "contractEndDate", javaType = LocalDate.class)
    })
    @Select("SELECT" +
            "   dc.PrincipalMail AS mail," +
            "   dc.`Name` AS companyName," +
            "   dc.Id AS companyId," +
            "   dm.`Name` AS mallName," +
            "   dm.Id AS mallId," +
            "   dm.ContractStartDate as contractStartDate," +
            "   dm.ContractEndDate as contractEndDate " +
            "FROM" +
            "   d_companies AS dc" +
            "   INNER JOIN d_malls AS dm ON dc.Id = dm.CompanyId " +
            "WHERE" +
            "   dm.ContractEndDate <= DATE_ADD( NOW(), INTERVAL 2 MONTH ) " +
            "   AND dm.ContractEndDate >= NOW() " +
            "   AND dm.Enabled = 1")
    List<Map<String, Object>> getAutomaticRenewalMalls();


    @Results({
            @Result(column = "mail", property = "mail"),
            @Result(column = "companyName", property = "companyName", javaType = String.class),
            @Result(column = "companyName", property = "companyName", javaType = String.class),
            @Result(column = "companyId", property = "companyId", javaType = String.class),
            @Result(column = "mallName", property = "mallName", javaType = String.class),
            @Result(column = "mallId", property = "mallId", javaType = String.class),
            @Result(column = "contractStartDate", property = "contractStartDate", javaType = LocalDate.class),
            @Result(column = "contractEndDate", property = "contractEndDate", javaType = LocalDate.class)
    })
    @Select("SELECT" +
            "   dc.PrincipalMail AS mail," +
            "   dc.`Name` AS companyName," +
            "   dc.Id AS companyId," +
            "   dm.`Name` AS mallName," +
            "   dm.Id AS mallId," +
            "   dm.ContractStartDate as contractStartDate," +
            "   dm.ContractEndDate as contractEndDate " +
            "FROM" +
            "   d_companies AS dc" +
            "   INNER JOIN d_malls AS dm ON dc.Id = dm.CompanyId " +
            "WHERE" +
            "   dm.ContractEndDate = DATE(DATE_ADD( NOW(), INTERVAL - 1 DAY )) " +
            "   AND dm.Enabled = 1")
    List<Map<String, Object>> getRenewalCompletedMalls();


    IPage<Map<String, Object>> selectRenewalRecordPage(Page page, @Param("ew") QueryWrapper queryWrapper);
}